<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="hexo,博客,hexo3,blog,github pages,免费个人博客" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="网上的一些教程操作mysql的要不是windows环境，或者是linux环境，在mac上搭建虽然和linux差不多，但是还是略有不同，下面是我遇到的一些问题： 密码重置mac环境利用安全模式重置mysql root的密码:基本的思路是，以安全模式启动mysql，这样不需要密码可以直接以root身份登录，然后重设密码。首先，我们停掉MySQL服务：1mysql.server stop 以安全模式启动">
<meta name="keywords" content="hexo,博客,hexo3,blog,github pages,免费个人博客">
<meta property="og:type" content="article">
<meta property="og:title" content="mysql初始安装的一些问题">
<meta property="og:url" content="http://lanbos.win/2015/03/10/python/mysql_base01/index.html">
<meta property="og:site_name" content="lanbos&#39;blog">
<meta property="og:description" content="网上的一些教程操作mysql的要不是windows环境，或者是linux环境，在mac上搭建虽然和linux差不多，但是还是略有不同，下面是我遇到的一些问题： 密码重置mac环境利用安全模式重置mysql root的密码:基本的思路是，以安全模式启动mysql，这样不需要密码可以直接以root身份登录，然后重设密码。首先，我们停掉MySQL服务：1mysql.server stop 以安全模式启动">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-03-29T08:47:48.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mysql初始安装的一些问题">
<meta name="twitter:description" content="网上的一些教程操作mysql的要不是windows环境，或者是linux环境，在mac上搭建虽然和linux差不多，但是还是略有不同，下面是我遇到的一些问题： 密码重置mac环境利用安全模式重置mysql root的密码:基本的思路是，以安全模式启动mysql，这样不需要密码可以直接以root身份登录，然后重设密码。首先，我们停掉MySQL服务：1mysql.server stop 以安全模式启动">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://lanbos.win/2015/03/10/python/mysql_base01/"/>





  <title> mysql初始安装的一些问题 | lanbos'blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">lanbos'blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">这个人假装很懒，什么都没有留下</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://lanbos.win/2015/03/10/python/mysql_base01/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="lanbos">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="https://ws1.sinaimg.cn/large/5c9d16d6gw1fbesf3zrhej209p09ftai.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="lanbos'blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="lanbos'blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                mysql初始安装的一些问题
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2015-03-10T08:55:29+08:00">
                2015-03-10
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/coder/" itemprop="url" rel="index">
                    <span itemprop="name">码畜相关</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>网上的一些教程操作mysql的要不是windows环境，或者是linux环境，在mac上搭建虽然和linux差不多，但是还是略有不同，下面是我遇到的一些问题：</p>
<h2 id="密码重置"><a href="#密码重置" class="headerlink" title="密码重置"></a>密码重置</h2><p>mac环境利用安全模式重置mysql root的密码:<br>基本的思路是，以安全模式启动mysql，这样不需要密码可以直接以root身份登录，然后重设密码。<br>首先，我们停掉MySQL服务：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql.server stop</div></pre></td></tr></table></figure></p>
<p>以安全模式启动MySQL：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysqld_safe --skip-grant-tables --skip-networking &amp;</div></pre></td></tr></table></figure></p>
<p>注意我们加了–skip-networking，避免远程无密码登录 MySQL。<br>这样我们就可以直接用root登录，无需密码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql -u root</div></pre></td></tr></table></figure></p>
<p>接着重设密码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; use mysql;  </div><div class="line">mysql&gt; update user set password=PASSWORD(&quot;newPassword&quot;) where User=&apos;root&apos;;  </div><div class="line">mysql&gt; flush privileges;  </div><div class="line">mysql&gt; quit</div></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>注意，命令后需要加分号。<br>重设完毕后，我们退出，然后启动 MySQL 服务：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql.server restart</div><div class="line">mysql -u root -p</div></pre></td></tr></table></figure></p>
<p>输入密码之后就进入了数据库。</p>
<h2 id="默认端口修改"><a href="#默认端口修改" class="headerlink" title="默认端口修改"></a>默认端口修改</h2><p>mac下通过brew安装mysql时是没有my.cnf配置文件的，简单的可以通过ruby一行命令来自动生成：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo cp $(brew --prefix mysql)/support-files/my-default.cnf /etc/my.cnf</div></pre></td></tr></table></figure></p>
<p>当然也可以自己创建一份<em>/etc/my.cnf</em>，附上linux的一份my.cnf:<br><figure class="highlight yml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># The MySQL database server configuration file.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># You can copy this to one of:</span></div><div class="line"><span class="comment"># - "/etc/mysql/my.cnf" to set global options,</span></div><div class="line"><span class="comment"># - "~/.my.cnf" to set user-specific options.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># One can use all long options that the program supports.</span></div><div class="line"><span class="comment"># Run program with --help to get a list of available options and with</span></div><div class="line"><span class="comment"># --print-defaults to see which it would actually understand and use.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># For explanations see</span></div><div class="line"><span class="comment"># http://dev.mysql.com/doc/mysql/en/server-system-variables.html</span></div><div class="line"></div><div class="line"><span class="comment"># This will be passed to all mysql clients</span></div><div class="line"><span class="comment"># It has been reported that passwords should be enclosed with </span></div><div class="line"><span class="comment"># ticks/quotes escpecially if they contain "#" chars...</span></div><div class="line"><span class="comment"># Remember to edit /etc/mysql/debian.cnf when changing </span></div><div class="line"><span class="comment"># the socket location.</span></div><div class="line"><span class="string">[client]</span></div><div class="line"><span class="string">port</span>        <span class="string">=</span> <span class="number">3306</span></div><div class="line"><span class="comment">#socket     = /var/run/mysqld/mysqld.sock</span></div><div class="line"></div><div class="line"><span class="comment"># Here is entries for some specific programs</span></div><div class="line"><span class="comment"># The following values assume you have at least 32M ram</span></div><div class="line"></div><div class="line"><span class="comment"># This was formally known as [safe_mysqld]. Both versions </span></div><div class="line"><span class="comment"># are currently parsed.</span></div><div class="line"><span class="string">[mysqld_safe]</span></div><div class="line"><span class="comment">#socket     = /var/run/mysqld/mysqld.sock</span></div><div class="line"><span class="comment">#nice       = 0</span></div><div class="line"></div><div class="line"><span class="string">[mysqld]</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * Basic Settings</span></div><div class="line"><span class="comment">#</span></div><div class="line"></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * IMPORTANT</span></div><div class="line"><span class="comment">#   If you make changes to these settings and your system uses </span></div><div class="line"><span class="comment">#   apparmor, you may also need to also adjust </span></div><div class="line"><span class="comment">#   /etc/apparmor.d/usr.sbin.mysqld.</span></div><div class="line"><span class="comment">#</span></div><div class="line"></div><div class="line"><span class="comment">#user       = mysql</span></div><div class="line"><span class="comment">#socket     = /var/run/mysqld/mysqld.sock</span></div><div class="line"><span class="string">port</span>        <span class="string">=</span> <span class="number">3306</span></div><div class="line"><span class="comment">#basedir    = /usr</span></div><div class="line"><span class="string">datadir</span>    <span class="string">=</span> <span class="string">/usr/local/var/mysql</span></div><div class="line"><span class="comment">#tmpdir     = /tmp</span></div><div class="line"><span class="string">skip-external-locking</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Instead of skip-networking the default is now to listen only on</span></div><div class="line"><span class="comment"># localhost which is more compatible and is not less secure.</span></div><div class="line"><span class="string">bind-address</span>        <span class="string">=</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * Fine Tuning</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="string">key_buffer</span>          <span class="string">=</span> <span class="number">16</span><span class="string">M</span></div><div class="line"><span class="string">max_allowed_packet</span>  <span class="string">=</span> <span class="number">16</span><span class="string">M</span></div><div class="line"><span class="string">thread_stack</span>        <span class="string">=</span> <span class="number">192</span><span class="string">K</span></div><div class="line"><span class="string">thread_cache_size</span>   <span class="string">=</span> <span class="number">8</span></div><div class="line"><span class="comment"># This replaces the startup script and checks MyISAM tables if needed</span></div><div class="line"><span class="comment"># the first time they are touched</span></div><div class="line"><span class="string">myisam-recover</span>         <span class="string">=</span> <span class="string">BACKUP</span></div><div class="line"><span class="comment">#max_connections       = 100</span></div><div class="line"><span class="comment">#table_cache           = 64</span></div><div class="line"><span class="comment">#thread_concurrency    = 10</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * Query Cache Configuration</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="string">query_cache_limit</span>   <span class="string">=</span> <span class="number">1</span><span class="string">M</span></div><div class="line"><span class="string">query_cache_size</span>    <span class="string">=</span> <span class="number">16</span><span class="string">M</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * Logging and Replication</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Both location gets rotated by the cronjob.</span></div><div class="line"><span class="comment"># Be aware that this log type is a performance killer.</span></div><div class="line"><span class="comment"># As of 5.1 you can enable the log at runtime!</span></div><div class="line"><span class="comment">#general_log_file        = /var/log/mysql/mysql.log</span></div><div class="line"><span class="comment">#general_log             = 1</span></div><div class="line"></div><div class="line"><span class="string">log_error</span>                <span class="string">=</span> <span class="string">/usr/local/var/mysql/MacBook15.local.err</span></div><div class="line"></div><div class="line"><span class="comment"># Here you can see queries with especially long duration</span></div><div class="line"><span class="comment">#log_slow_queries   = /var/log/mysql/mysql-slow.log</span></div><div class="line"><span class="comment">#long_query_time = 2</span></div><div class="line"><span class="comment">#log-queries-not-using-indexes</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># The following can be used as easy to replay backup logs or </span></div><div class="line"><span class="comment"># for replication.</span></div><div class="line"><span class="comment"># <span class="doctag">note:</span> if you are setting up a replication slave, see </span></div><div class="line"><span class="comment">#       README.Debian about other settings you may need </span></div><div class="line"><span class="comment">#       to change.</span></div><div class="line"><span class="comment">#server-id          = 1</span></div><div class="line"><span class="comment">#log_bin            = /var/log/mysql/mysql-bin.log</span></div><div class="line"><span class="string">expire_logs_days</span>    <span class="string">=</span> <span class="number">10</span></div><div class="line"><span class="string">max_binlog_size</span>     <span class="string">=</span> <span class="number">100</span><span class="string">M</span></div><div class="line"><span class="comment">#binlog_do_db       = include_database_name</span></div><div class="line"><span class="comment">#binlog_ignore_db   = include_database_name</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * InnoDB</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.</span></div><div class="line"><span class="comment"># Read the manual for more InnoDB related options. There are many!</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># * Security Features</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Read the manual, too, if you want chroot!</span></div><div class="line"><span class="comment"># chroot = /var/lib/mysql/</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># For generating SSL certificates I recommend the OpenSSL GUI "tinyca".</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># ssl-ca=/etc/mysql/cacert.pem</span></div><div class="line"><span class="comment"># ssl-cert=/etc/mysql/server-cert.pem</span></div><div class="line"><span class="comment"># ssl-key=/etc/mysql/server-key.pem</span></div><div class="line"></div><div class="line"><span class="comment"># Query Caching</span></div><div class="line"><span class="string">query-cache-type</span> <span class="string">=</span> <span class="number">1</span></div><div class="line"></div><div class="line"><span class="comment"># Default to InnoDB</span></div><div class="line"><span class="string">default-storage-engine=innodb</span></div><div class="line"></div><div class="line"><span class="string">[mysqldump]</span></div><div class="line"><span class="string">quick</span></div><div class="line"><span class="string">quote-names</span></div><div class="line"><span class="string">max_allowed_packet</span>  <span class="string">=</span> <span class="number">16</span><span class="string">M</span></div><div class="line"></div><div class="line"><span class="string">[mysql]</span></div><div class="line"><span class="comment">#no-auto-rehash # faster start of mysql but no tab completition</span></div><div class="line"></div><div class="line"><span class="string">[isamchk]</span></div><div class="line"><span class="string">key_buffer</span>      <span class="string">=</span> <span class="number">16</span><span class="string">M</span></div></pre></td></tr></table></figure></p>
<h2 id="mysql常用命令"><a href="#mysql常用命令" class="headerlink" title="mysql常用命令"></a>mysql常用命令</h2><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">#查看端口号</div><div class="line">show variables like 'port';</div><div class="line">#创建数据库</div><div class="line">create database name;</div><div class="line">#查看数据库</div><div class="line">show databases;</div><div class="line">show create database name; </div><div class="line">#删除数据库</div><div class="line">drop database name;</div><div class="line">#进入数据库</div><div class="line">use  name;</div><div class="line">#查看当前数据库名称</div><div class="line">select database();</div><div class="line">#创建数据表</div><div class="line">create table name(username varchar(20),age tinyint unsigned,salary float(9,2) unsigned);</div><div class="line">#查看数据表</div><div class="line">show tables;</div><div class="line">show tables from name;</div><div class="line">#查看条目</div><div class="line">show columns from name;</div><div class="line">#插入记录</div><div class="line">insert name values('tom',25,7681.10);</div><div class="line">#插入所选记录的内容</div><div class="line">insert tb1(username,salary) values('john',4500.88);</div><div class="line">#用表达式插入单挑记录指定字段（set）</div><div class="line">instert tb1 set username='tom',salary='8000';</div><div class="line">#查看表详细内容</div><div class="line">select * from name;</div><div class="line">#添加单列</div><div class="line">alter table name add cName tinyint unsigned not null default 10;</div><div class="line">#删除单列</div><div class="line">alter table name drop cName;</div><div class="line">#修改列定义(modify和change)</div><div class="line">alter table name modify int(2) first;</div><div class="line">alter table name change name newName int(2);</div><div class="line">#更新记录</div><div class="line">update name set age=10,pwd='123' where id=1;</div><div class="line">#查询语句</div><div class="line">select name.id,name.username form name;</div><div class="line">#查询分组</div><div class="line">select name.id,name.sex from name group by sex;</div><div class="line">#查询分组条件</div><div class="line">select name.id,name.sex from name group by sex having id&gt;2;</div><div class="line">#查询结果分组,前面字段若有重复按照第二字段继续排序</div><div class="line">select * from name order by age, id desc;</div><div class="line">#限制查询结果条目数 从0 开始后的3条</div><div class="line">select * from name limit 0,3;</div><div class="line">#有条件的插入其他表中的记录到当前表</div><div class="line">insert name(age) select age from othername where age&gt;8 limit 1,2;</div></pre></td></tr></table></figure>
<h2 id="mysql-约束"><a href="#mysql-约束" class="headerlink" title="mysql 约束"></a>mysql 约束</h2><p>非空约束<em>not null</em><br>主键约束<em>primary key</em><br>唯一约束<em>unique key</em><br>默认约束<em>default</em><br>外键约束<em>foreign key</em></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/mysql/" rel="tag"># mysql</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/02/13/hexo/hexo_decorateBlog3/" rel="next" title="hexo装饰(三)">
                <i class="fa fa-chevron-left"></i> hexo装饰(三)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/10/02/tools/markdown_base/" rel="prev" title="markdown语法">
                markdown语法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>
  <!-- UY BEGIN -->
<div id="uyan_frame"></div>
<script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2123221"></script>
<!-- UY END -->



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://ws1.sinaimg.cn/large/5c9d16d6gw1fbesf3zrhej209p09ftai.jpg"
               alt="lanbos" />
          <p class="site-author-name" itemprop="name">lanbos</p>
          <p class="site-description motion-element" itemprop="description">这个人假装很懒，什么都没有留下</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">24</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#密码重置"><span class="nav-number">1.</span> <span class="nav-text">密码重置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#默认端口修改"><span class="nav-number">2.</span> <span class="nav-text">默认端口修改</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql常用命令"><span class="nav-number">3.</span> <span class="nav-text">mysql常用命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql-约束"><span class="nav-number">4.</span> <span class="nav-text">mysql 约束</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lanbos</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	




  
  

  

  

  

  


</body>
</html>
